<?php
include(APP_HOME."/modules/systemapi/controllers/browser.php");
if(!class_exists('MAIN_Controller_Action')) Zend_Loader :: loadClass('MAIN_Controller_ActionFront');
class Front_PreauthorizeController extends MAIN_Controller_ActionFront{
	public function preauthorizeAction() {
		$view = $this->_getView();
		$generic = $this->getGeneric();
		$data = $generic->getFund();
		$view->assign("data",$data);
		$view->output('preauthorize/preauthorize.tpl');
    }
    public function loadtplAction(){
    	$params   = $this->getParams();
    	$view = $this->_getView();
    	$view->output('preauthorize/'.$params['tpl'].'.tpl');
    }
    public function getpreauthbyfundAction(){
    	$params   = $this->getParams();
    	$generic = $this->getGeneric();
    	$view = $this->_getView();
    	$data = $generic->getpreauthbyfund($params);
    	$view->assign("data",$data);
    	$data['htmls'] = $view->outputhtml('ajax/_view_preauth_list.tpl');
    	$this->_helper->json($data);
    }
    public function preauthdetailAction(){
    	$params   = $this->getParams();
    	$generic = $this->getGeneric();
    	$view = $this->_getView();
    	$today = getToday();
    	//$preauthId = $generic->getPreauthId();
    	$policyData = $generic->getPolicyById($params['policy_id']);
    	$paInfo = $generic->getPaInfo($params['policy_id']);
    	$deducList = $generic->getDeducList($params['policy_id']);

    	if(empty($deducList)){
    		$deducList[0]['deducper'] = '-';
    		$deducList[0]['dgroup'] = $policyData[effective];
    		$deducList[0]['deduc'] = number_format($policyData[deductible],2,'.',',');
    	}
    	else{
    		foreach ($deducList as $key=>$value){   			
    			$deducList[$key]['deduc'] = number_format(($policyData[deductible] - $deducList[$key]['deduc']),2,'.',',');
    			
    		}
    	}
    	$policyData[deductible] = number_format($policyData[deductible],2,'.',',');

    	if($policyData['customer_type_desc'] == 'PA'){
    		$paClaimData = $generic->getPaClaim($params['policy_id']);
    		$tpl = 'preauthorize/pa_detail.tpl';
    		
    	}else if($policyData['customer_type_desc'] == 'Health'){
    		$paClaimData = $generic->getPaClaimOpd($params['policy_id']);
    		$erClaimData = $generic->getErClaim($params['policy_id']);
    		$PlanData = $generic->getPlan2catByPlanId($policyData['plan_id'],$params['policy_id']); 
    		$PlanData['opd'] = number_format($PlanData['opd'],2,'.',',');
    		$PlanData['rb'] = number_format($PlanData['rb'],2,'.',',');
    		$PlanData['er'] = number_format($PlanData['er'],2,'.',',');
    		$view->assign("erClaimData",$erClaimData);
    		$view->assign("PlanData",$PlanData);
    		$tpl = 'preauthorize/health_detail.tpl';
    	}else{   		
    		echo "<script type='text/javascript'>alert('กรุณาติดต่อ HBC')</script>";
    		echo "<script langquage='javascript'>window.location='".APP_URL."/preauthorize/'</script>";
    	}
    	
    	$view->assign("policy_id",$params['policy_id']);
    	$view->assign("today",$today);
    	//$view->assign("preauthId",$preauthId);
    	$view->assign("policyData",$policyData);
    	$view->assign("paInfo",$paInfo);
    	$view->assign("paClaimData",$paClaimData);
    	$view->assign("deducList",$deducList);
    	$view->assign("title",'Pre-authorize');
    	$view->output($tpl);
    }
    public function getpreauthconfirmAction(){
    	$params   = $this->getParams();
    	$view = $this->_getView();
    	$data['today']	= date("Y-m-d H:i:s");
    	$data['policy_id'] = $params['policy_id'];
    	$data['policy_no'] = $params['policy_no'];
    	$data['card_no'] = $params['card_no'];
    	$data['member_name'] = $params['member_name'];
    	$data['member_lname'] = $params['member_lname'];
    	$data['nat_id'] = $params['nat_id'];
    	$data['preauth_id'] = $params['preauth_id'];
    	$data['customer_type_desc'] = $params['customer_type_desc'];
    	$view->assign("data",$data);
    	$data['htmls'] = $view->outputhtml('ajax/_view_preauth_detail_confirm.tpl');
    	$this->_helper->json($data);
    }
    function get_client_ip() {
    	$ipaddress = '';
    	if ($_SERVER['HTTP_CLIENT_IP'])
    		$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    	else if($_SERVER['HTTP_X_FORWARDED_FOR'])
    		$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    	else if($_SERVER['HTTP_X_FORWARDED'])
    		$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    	else if($_SERVER['HTTP_FORWARDED_FOR'])
    		$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    	else if($_SERVER['HTTP_FORWARDED'])
    		$ipaddress = $_SERVER['HTTP_FORWARDED'];
    	else if($_SERVER['REMOTE_ADDR'])
    		$ipaddress = $_SERVER['REMOTE_ADDR'];
    	else
    		$ipaddress = 'UNKNOWN';
    
    	return $ipaddress;
    }
    public function insertpreauthAction(){
    	$params = $this->getParams();
    	$generic = $this->getGeneric();
    	$preauthId = $generic->getPreauthId();
    	
    	$browser = new Browser();
    	$get_browser = $browser->getBrowser();
    	$get_browser_version = $browser->getVersion();
    	
    	$client_ip = file_get_contents('http://phihag.de/ip/');
    	$client_ip = $client_ip.'-'.$this->get_client_ip();
    	//echo 'You are using ', $browser->getBrowser(), ' version ', $browser->getVersion();
    	$generic->insertPreauth($params,$preauthId,$get_browser.$get_browser_version,$client_ip);
    	
    }
    public function seachidcardAction(){
    	$view = $this->_getView();
    	$generic = $this->getGeneric();
    	$data = $generic->getFund();
    	$view->assign("data",$data);
    	$view->output('preauthorize/search_idcard.tpl');
    }
    public function getpreauthbyidcardAction(){
    	$params   = $this->getParams();
    	$generic = $this->getGeneric();
    	$view = $this->_getView();
    	$data = $generic->getpreauthbyidcard($params);
    	$view->assign("data",$data);
    	$data['htmls'] = $view->outputhtml('ajax/_view_preauth_list.tpl');
    	$this->_helper->json($data);
    }
}
?>